<?php
/**
 * User: liubao
 * Date: 19-4-30
 * Time: 下午4:08
 */

namespace App\Controller;


use App\Service\UserService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class UserController extends AbstractController
{
    /**
     * @Route("/user/login", methods={"POST"})
     */
    public function login(Request $request): Request
    {
//        print_r($request->request->all());die; //4.2这样获取已经获取不到了
        $params = json_decode($request->getContent(), true);
        $res = UserService::doLogin($params);
        if (!empty($res)) {;
            $res = array('code' => 20000, 'data' => $res, 'msg' => '请求参数成功', 'success' => true);
            echo json_encode($res, JSON_UNESCAPED_UNICODE);
            die;
        } else {
            $res = array('code' => 500, 'data' => $res, 'msg' => '账号活密码有误', 'success' => false);
            echo json_encode($res, JSON_UNESCAPED_UNICODE);
            die;
        }
    }

    /**
     * @Route("user/info",methods={"GET"})
     */
    public function getUserInfo(Request $request): Request
    {
        $token = $request->query->get('token');
        if ($token === 'admin-token'){
            $res = array(
                'avatar' => '',
                'introduction' => 'I am a super administrator',
                'name' => 'Super Admin',
                'roles' => array(0=>'admin')
            );
            $res = array('code' => 20000, 'data' => $res, 'msg' => '请求参数成功', 'success' => true);
            echo json_encode($res, JSON_UNESCAPED_UNICODE);
            die;
        }else{
            $res = array('code' => 500, 'data' => [], 'msg' => '未知错误', 'success' => false);
            echo json_encode($res, JSON_UNESCAPED_UNICODE);
            die;
        }
    }

    /**
     * @Route("user/logout")
     */
    public function layout()
    {
        UserService::clearSession(null);
        $res = array('code' => 20000, 'data' => [], 'msg' => '请求参数成功', 'success' => true);
        echo json_encode($res, JSON_UNESCAPED_UNICODE);
        die;

    }


    protected function userModel()
    {
        return new UserService();
    }

}
